<template>
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <!--查询区域-->
    <div class="">
      <a-form
        ref="formRef"
        class="jeecg-basic-table-form"
        @keyup.enter.native="reload"
        :model="queryParam"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-row :gutter="24">
          <a-col :lg="6">
            <a-form-item label="委托单位" name="entName">
              <!-- <j-dict-select-tag placeholder="" v-model:value="queryParam.entName" :show-choose-option="false"
                dictCode="lab_enterprise,ent_name,ent_name" /> -->
              <j-input placeholder="" v-model:value="queryParam.entName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item label="样品大类" name="sampleType">
              <j-dict-select-tag
                placeholder=""
                v-model:value="queryParam.sampleType"
                :show-choose-option="false"
                dictCode="lab_items,name,id,type='A'"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="reload">查询</a-button>
                <a-button preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
                <a @click="toggleSearchStatus = !toggleSearchStatus" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <Icon :icon="toggleSearchStatus ? 'ant-design:up-outlined' : 'ant-design:down-outlined'" />
                </a>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :expandedRowKeys="expandedRowKeys" @expand="handleExpand">
      <!-- 内嵌table区域 begin -->
      <template #expandedRowRender="{ record }">
        <a-tabs tabPosition="top">
          <a-tab-pane tab="实验项目" key="labItems" forceRender>
            <LabItemsSubTable :id="expandedRowKeys[0]" />
          </a-tab-pane>
        </a-tabs>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 结果报告-->
    <TableReportModal @register="registerReportModal" @reporting="downloadt" :disabledPreview="disabledPreview" @success="handleSuccess"></TableReportModal>
    <!-- 审核modal -->
    <TableReviewModal @register="registerReviewModal" @success="handleSuccess"></TableReviewModal>
  </div>
</template>

<script lang="ts" name="items-report" setup>
  import { ref, reactive } from 'vue';
  import { JInput, JDictSelectTag } from '/@/components/Form';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import { columns } from './LabItemReportList.data';
  import { list } from './LabItemReportList.api';
  import axios from 'axios';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import LabItemsSubTable from './subTables/LabItemsSubTable.vue';
  import TableReportModal from './LabItemReportFrom.vue';
  import TableReviewModal from './LabItemReviewFrom.vue';

  const formRef = ref();
  // 展开key
  const expandedRowKeys = ref<any[]>([]);
  const queryParam = reactive<any>({
    billStatus: '待编辑',
  });
  //注册model
  const [registerReportModal, { openModal: openRModal }] = useModal();
  //注册model
  const [registerReviewModal, { openModal }] = useModal();
  let disabledPreview = ref(true);

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      api: list,
      columns: columns,
      canResize: false,
      useSearchForm: false,
      showIndexColumn: true,
      showTableSetting: false,
      actionColumn: {
        width: 80,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  });

  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

  /**
   * 展开事件
   * */
  function handleExpand(expanded, record) {
    expandedRowKeys.value = [];
    if (expanded === true) {
      expandedRowKeys.value.push(record.id);
    }
  }

  const instancea = axios.create({
    baseURL: 'http://10.89.208.24:9999',
    headers: {
      responseType: 'blob',
      ContentType: 'multipart/json, charset=UTF-8',
      timeout: 1000000,
      type: 'multipart/form-data',
    },
  });
  /**
   *  下载并返回。
   */
  function downloadt(e) {
    instancea.defaults.responseType = 'blob';
    instancea
      .post('/entrust/labEntrust/downloadWord/' + e.id + '/' + e.entrustType)
      .then((response) => {
        console.log('生成报告成功', response);
      })
      .catch((error) => {
        // 处理错误
        console.error(error);
      });
  }

  function handleEdit(record) {
    openRModal(true, {
      record,
      disabled: false,
      isUpdate: true,
    });
  }

  function handleRecord(record) {
    openModal(true, {
      record,
    });
  }

  function handleReport(record) {
    openModal(true, {
      record,
    });
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  function getTableAction(record) {
    if (record.billStatus == '待编辑' || record.billStatus == null) {
      return [
        {
          label: '新增报告',
          onClick: handleEdit.bind(null, record),
        },
      ];
    } else if (record.billStatus == '待审核') {
      return [
        {
          label: '审核报告',
          onClick: handleRecord.bind(null, record),
        },
      ];
    } else if (record.billStatus == '待批准') {
      return [
        {
          label: '批准报告',
          onClick: handleRecord.bind(null, record),
        },
      ];
    } else {
      return [
        {
          label: '生成报告',
          onClick: downloadt.bind(null, record),
          disabled: false,
        },
      ];
    }
  }

  /* ----------------------以下为原生查询需要添加的-------------------------- */
  const toggleSearchStatus = ref<boolean>(false);
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });
  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
</script>
<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }

    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }

    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }

  // .jeecg-basic-table-form {
  //   height: 58px;
  // }

  :deep(.ant-table-title) {
    display: none;
  }
</style>
